<?php
class HayApiMsgDatabase extends CActiveRecord{
	public $Tablename;
	public $Array;
	public $SearchText;
	public $LastId;
	public $Limit;
	public $ListLtype;
	public $Field;
	public $SearField;
	public $UidArray = array(18,19,20,21,22,23);
	public function __construct($Tablename=''){
		$this->Tablename = $Tablename;
		
	}
	public function ModelObject(){
		$model = new $this->Tablename();
		$model = $model->findAllBySql($this->getSql());
		$array = array();
		$array['data']['code']=1;
		foreach ($model as $value){
			$arr = array();
			foreach ($this->Array as $key=>$v){
				$a[$key] = CHtml::value($value, $v);
				$arr = $a;
			}
			$array['data'][] = $arr;
		}
		return $array;
	}
	
	public function getSql(){
		
		$sql = "SELECT * FROM {$this->Tablename} WHERE {$this->Field} = {$this->UidArray[$this->ListLtype]} AND {$this->SearField}={$this->LastId}";
		return $sql;
	}
	public function scopes(){
		return array(
		    'published'=>array(
		          'condition'=>$this->Field.'='.$this->UidArray[$this->ListLtype],
		    ),
		    'recently'=>array(
		          'limit'=>$this->Limit,
		    ),
		);
	}
	public function published(){
		$this->getDbCriteria()->mergeWith(
			array(
			     'condition'=>$this->Field.'='.$this->UidArray[$this->ListLtype],
			));
		return $this;
	}
	
	public function recently(){
		$this->getDbCriteria()->mergeWith(array(
			 'limit'=>$this->Limit,
		));
	}
	
	public function JsonDatas(){
		if ($this->LastId != ''){
			echo json_encode($this->ModelObject());
			
		}else {
			$array = array(
				'code'=>0,
				'msg'=>'listID为空',
			);
			echo json_encode($array);
		}
		
	}
	
}
?>